function Q(id){
    return document.getElementById(id);
}

function setCopy(text, msg){
	var browser = navigator.userAgent;
    if(browser == 'msie') {
		clipboardData.setData('Text', text);
		alert(msg);
	} else if(prompt('Press Ctrl+C Copy to Clipboard', text)) {
		alert(msg);
	}
}

function findSWF(movieName){
    if(navigator.appName.indexOf("Microsoft")!= -1){
        return window["ie_" + movieName];
    }else{
        return document[movieName];
    }
}

function ipToCountry(ip){
    var url = APP + '/Ajax/ipToCountry/ip/' + ip;
    $.dialog.load(url, {
        id: 999,
        title: LANG.view_ip_address,
        width: 320,
        height: 80
    }, true);
}

function goToByScroll(dom){
	$('html,body').animate({scrollTop: $(dom).offset().top},'slow');
}

function reCaptcha(obj){
    var now = new Date().getTime();
    var src = APP + '/Ajax/captcha/rand/' + now;
    $(obj).attr('src', src);
}

function checkLogin(){
    if(UID == 0){
        window.location.href = LOGIN_URL;
    }
    return false;
}

function getCategoryOptions(obj,child){
    var id = $(obj).attr('id');
    var cid = parseInt($(obj).val());
    var postUrl = APP +'/Ajax/getCategoryOptions/cid/'+ cid;
    $('#category-id').val(cid);
    if(cid == 0){
        if(id == 'first-level'){
            $('#second-level,#third-level').html('').hide();
        }else if(id == 'second-level'){
            $('#third-level').html('').hide();
        }
        return;
    }
    if(id == 'third-level'){
    	return;	
    }
    $.post(postUrl,function(json){
        var data = eval('('+ json +')');
        var options = '<option value="0">'+ LANG.all_sub_categories +'</option>';
        if(data.length > 0){
            for(var i in data){
                options += '<option value="'+ data[i].category_id +'">'+ data[i].name +'</option>';
            }
            $('#'+child).html(options).show();
        }else{
            $('#'+child).html(options).hide();
        }
    });
}

function editQuestionDialog(qid){
    goToByScroll('#page-header');
    setTimeout(function(){
        var url = APP + '/Question/edit/qid/' + qid;
        $.dialog.load(url, {
            top: 50,
            left: 250,
            id: qid,
            title: LANG.edit_question
        }, true);
    }, 500);
}

function shareQuestionDialog(qid){
    checkLogin();
    var url = APP + '/Question/share/qid/' + qid;
    $.dialog.load(url, {
        id: qid,
        title: LANG.share_question
    }, true);
}

function addAnswerDialog(qid, type){
    var url = APP + '/Answer/add/qid/' + qid + '/type/' + type;
    var title = (type == 'normal') ? LANG.answer_question : LANG.improve_answer;
    $.dialog.load(url, {
        id: qid,
        title: title
    }, true);
}

function editAnswerDialog(aid, isTeamwork){
    var url = APP + '/Answer/edit/aid/' + aid;
    var title = (isTeamwork == 0) ? LANG.edit_answer : LANG.improve_answer;
    $.dialog.load(url, {
        id: aid,
        title: title
    }, true);
}

function delQuestionDialog(aid){
    var url = APP + '/Question/delete/qid/' + qid;
    var msg = LANG.delete_question;
    if(window.confirm(msg)){
        window.location.href = url;
    }
    return false;
}

function delAnswerDialog(aid){
    var url = APP + '/Answer/delete/aid/' + aid;
    var msg = LANG.delete_answer;
    if(window.confirm(msg)){
        window.location.href = url;
    }
    return false;
}

function delCommentDialog(cid){
    var url = APP + '/Comment/delete/cid/' + cid;
    var msg = LANG.delete_comment;
    if(window.confirm(msg)){
        window.location.href = url;
    }
}

function chooseBestAnswerDialog(aid){
    var url = APP + '/Answer/chooseBestAnswer/aid/' + aid;
    $.dialog.load(url, {
        id: aid,
        title: LANG.choose_best_answer
    }, true);
}

function addDetailsDialog(qid, type){
    var url = APP + '/Question/addDetails/qid/' + qid;
    $.dialog.load(url, {
        id: qid,
        title: LANG.add_details
    }, true);
}

function addCommentDialog(rid, type){
    var url = APP + '/Comment/add/rid/' + rid + '/type/' + type;
    $.dialog.load(url, {
        id: rid,
        title: LANG.add_comment
    }, true);
}

function editCommentDialog(cid){
    var url = APP + '/Comment/edit/cid/' + cid;
    $.dialog.load(url, {
        id: cid,
        title: LANG.edit_comment
    }, true);
}

function resetPasswordDialog(){
    var url = APP + '/Account/forgetPassword';
    $.dialog.load(url, {
        id: 'reset',
        title: LANG.reset_password
    }, true);
}

function pmDialog(uid){
	var pmUrl = APP + '/User/pm/uid/' + uid;
	var postUrl = APP + '/Ajax/checkPmPerms/uid/' + uid;
    $.post(postUrl, function(json){
    	var json = eval('('+ json +')');
    	if(json.status == 1){
    		$.dialog.load(pmUrl, {
		        id: uid,
		        title: LANG.send_pm
		    }, true);
    	}else{
    		$.dialog.tips(json.info);		
    	}
    });
    
}

function reportDialog(rid, type){
    var url = APP + '/Report/add/rid/' +rid+ '/type/' +type;
    $.dialog.load(url, {
        id: rid,
        title: LANG.report_abuse
    }, true);
}

function approveDetailsDialog(rid, type){
    var url = APP + '/Modcp/approveDetails/rid/' +rid+ '/type/' +type;
    $.dialog.load(url, {
        id: rid,
        title: LANG.approve_details
    }, true);
}

function voteBestAnswer(qid, aid){
    var url = APP + '/Answer/voteBestAnswer/qid/'+ qid + '/aid/'+ aid;
    window.location.href = url;
}

function chooseBestAnswer(qid, aid){
    var url = APP + '/Answer/chooseBestAnswer/qid/'+ qid + '/aid/'+ aid;
    window.location.href = url;
}

function voteNoBestAnswer(qid){
    var url = APP + '/Answer/voteNoBestAnswer/qid/'+ qid;
    window.location.href = url;
}

function starQuestion(obj,qid){
    var url = APP + '/Ajax/starQuestion';
    var data = {qid: qid};
    $(obj).attr('class','staring');
    $.post(url, data, function(json){
        var json = eval('('+json+')');
        if(json.status == 0){
            $.dialog.tips(json.info, 3);
        }else{
            $('#star-count').text(json.star_count);
        }
    });
}

function rateAnswer(obj, aid, type){
    var url = APP + '/Ajax/rateAnswer';
    var data = {aid: aid, type: type};
    $.post(url, data, function(json){
        var json = eval('(' + json + ')');
        if(json.status == 0){
            $.dialog.tips(json.info, 3);
        }
        $('#rate-'+aid+'>li').removeAttr('onclick');
        $('#rate-'+aid).attr('class','rated');
        $('#rate-'+aid+'>.up>span').text(json.up_rate_count);
        $('#rate-'+aid+'>.down>span').text(json.down_rate_count);
        return false;
    });
}

function loadAnswerRevision(obj, div){
	var url = APP + '/Ajax/loadAnswerRevision';
	var data = {id: $(obj).val()};
	$.post(url, data, function(json){
		var json = eval('(' + json + ')');
		$('#'+ div).html(json.details);
	});	
}

function followQuestion(qid){
    var url = APP + '/Ajax/followQuestion';
    var data = {qid: qid};
    $.post(url, data, function(json){
        var json = eval('(' + json + ')');
        if(json){
            $.dialog.tips(json.info, 3);
            $('#follow-question>a').removeAttr('onclick');
        }
    });
}

function followUser(obj, uid){
    var url = APP + '/Ajax/followUser';
    var data = {uid: uid};
    $.post(url, data, function(json){
        var json = eval('(' + json + ')');
        $.dialog.tips(json.info, 3);
        $(obj).removeAttr('onclick').removeAttr('href');
    });
}

function blockUser(obj, uid){
    var url = APP + '/Ajax/blockUser';
    var data = {uid: uid};
    $.post(url, data, function(json){
        var json = eval('(' + json + ')');
        $.dialog.tips(json.info, 3);
        $(obj).removeAttr('onclick').removeAttr('href');
        return false;
    });
}
